import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:winkai/res/app_colors.dart';
import 'package:winkai/res/dimens.dart';
import 'package:winkai/ui/msg/controller/chat_controller.dart';
import 'package:winkai/ui/msg/widget/chat_topic_widget.dart';
import 'package:winkai/utils/image_utils.dart';
import 'package:winkai/utils/smart_dialog.dart';

class ChatBottomBar extends StatefulWidget {
  const ChatBottomBar({super.key, required this.controller});

  final ChatController controller;

  @override
  State<ChatBottomBar> createState() => _ChatBottomBarState();
}

class _ChatBottomBarState extends State<ChatBottomBar> {
  late ChatController controller;
  late TextEditingController textEditingController;
  bool isSend = false;

  @override
  void initState() {
    super.initState();
    controller = widget.controller;
    textEditingController = TextEditingController();
    textEditingController.addListener(_onInputChange);
  }

  void _onInputChange() {
    isSend = textEditingController.text.isNotEmpty;
    setState(() {});
  }

  @override
  void dispose() {
    super.dispose();
    textEditingController.removeListener(_onInputChange);
  }

  @override
  Widget build(BuildContext context) {
    var inputWidget = Container(
      decoration: const BoxDecoration(
          borderRadius: BorderRadius.vertical(top: Radius.circular(32)),
          color: Colors.white),
      clipBehavior: Clip.antiAlias,
      child: Stack(
        children: [
          SafeArea(
            top: false,
            left: false,
            right: false,
            child: Container(
                margin: EdgeInsets.symmetric(
                    horizontal: Dimens.mainHorizontalSpacing, vertical: 24.w),
                padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.w),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(16),
                  border: Border.all(width: 1.w, color: Colors.black),
                ),
                child: Row(
                  children: [
                    _buildTopicWidget(),
                    Flexible(
                      child: TextField(
                        textInputAction: TextInputAction.send,
                        onEditingComplete: () {
                          onSendMessage(textEditingController.text);
                        },
                        minLines: 1,
                        maxLines: null,
                        style: TextStyle(
                          height: 1,
                          color: Colors.black,
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w500,
                        ),
                        controller: textEditingController,
                        decoration: InputDecoration(
                          hintText: 'chat_input_hint'.tr,
                          hintStyle: const TextStyle(color: AppColors.color_b3),
                          fillColor: Colors.transparent,
                          border: InputBorder.none,
                          filled: true,
                          isDense: true,
                          contentPadding: EdgeInsets.only(
                              left: 5.w, top: 6.w, right: 5.w, bottom: 6.w),
                        ),
                        autofocus: false,
                      ),
                    ),
                    _buildSendWidget(),
                  ],
                )),
          ),
        ],
      ),
    );
    return Platform.isIOS
        ? inputWidget
        : AnnotatedRegion<SystemUiOverlayStyle>(
            value: const SystemUiOverlayStyle(
                systemNavigationBarColor: Colors.white,
                systemNavigationBarDividerColor: Colors.white),
            child: inputWidget);
  }

  Widget _buildTopicWidget() {
    return GestureDetector(
      onTap: () {
        showChatTopicPop(context, onSendMessage: onSendMessage);
      },
      behavior: HitTestBehavior.translucent,
      child: ImageUtils.getImage('ic_ai_chat_tips', 32.w, 32.w),
    );
  }

  Widget _buildSendWidget() {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        if (isSend) {
          onSendMessage(textEditingController.text);
        }
      },
      child: ImageUtils.getImage(
          isSend ? 'ic_ai_chat_send_s' : 'ic_ai_chat_send', 32.w, 32.w),
    );
  }

  void onSendMessage(String content) {
    if (content.trim().isNotEmpty) {
      if (controller.sendMsg(content)) {
        textEditingController.clear();
      }
    } else {
      toast('chat_input_send_content'.tr);
    }
  }
}
